package connect

import (
	"github.com/spf13/viper"
	"go-chat-room/pkg/webScoket"
	_ "net/http/pprof"
)

func init() {
	defaultServer = NewServer()
	defaultServer.RegisterMsgHandler("init", defaultServer.HandlerMsgInit)
	defaultServer.RegisterMsgHandler("group", defaultServer.HandlerMsgGroup)
}

var (
	defaultServer *Server
)

type Connect struct {
}

func NewConnect() *Connect {
	return &Connect{}
}

func (connect *Connect) Run() {
	defaultServer.room = NewRoom()

	var sereverOptions = []webScoket.WebScoketOption{
		webScoket.WithPattern(viper.GetString("WebSocket.Pattern")),
		webScoket.WithAddr(viper.GetString("WebSocket.Addr")),
		webScoket.WithReadBufferSize(viper.GetInt("WebSocket.ReadBufferSize")),
		webScoket.WithWriteBufferSize(viper.GetInt("WebSocket.WriteBufferSize")),
		webScoket.WithBroadcastSize(viper.GetInt("WebSocket.BroadcastSize")),
		webScoket.WithMaxMessageSize(viper.GetInt64("WebSocket.MaxMessageSize")),
		webScoket.WithWriteWait(viper.GetDuration("WebSocket.WriteWait")),
		webScoket.WithPingPeriod(viper.GetDuration("WebSocket.PingPeriod")),
		webScoket.WithPongWait(viper.GetDuration("WebSocket.PongWait")),
	}

	server := webScoket.NewWebScoket(sereverOptions)

	server.ConnFunc = defaultServer.HandlerConnect

	server.DisConnFunc = defaultServer.HandlerDisConnect

	server.ReadFunc = defaultServer.HandlerRead

	server.Serve()
}
